package cn.stylefeng.guns.sys.modular.tao.shiUtil;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


/**
 * 文件上传方法
 */
public class UploadFileUtils {


    public static ResponseMsg uploadFile(MultipartFile file) {


        ResponseMsg msg = new ResponseMsg();


        try {
            try {
                System.out.println("fileName："+file.getOriginalFilename());
                String fileName = file.getOriginalFilename().replaceAll(",","");
                System.out.println("替换后的文件名"+fileName);
                String fileExt = fileName.substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                String newFileName = df.format(new Date()) + "_" + new Random().nextInt(10000) + "." + fileExt;
                String path=ToolUtils.getTempImgUrl()+newFileName;

                File newFile=new File(path);

                // 上传文件类型判断
                UploadResult result = new UploadResult();
                result.setFormat(getFileType(fileExt));

                // 文件格式错误，不允许上传
                if(null == result.getFormat()) {

                    msg.setResponseError(SysErrorCode.FILE_ERROR);
                    return msg;
                }


                if(SystemConstant.PHOTO.equals(result.getFormat())) {
                    // 压缩后上传
                    InputStream in = file.getInputStream();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    FileOutputStream out = null;
                    byte[] buff = new byte[10*1024];
                    int len = 0;
                    while((len=in.read(buff)) > 0) {
                        os.write(buff, 0, len);
                    }
                    os.flush();
                    byte[] results = Gzip.compressPicForScale(os.toByteArray(), 100);
                    out = new FileOutputStream(newFile);
                    out.write(results);
                    out.flush();

                    in.close();
                    out.close();
                }else {

                    //通过CommonsMultipartFile的方法直接写文件
                    file.transferTo(newFile);
                }


                // 上传到腾讯云
                String uploadFile = TencentCloud.uploadFile(TencentCloud.Bucket,newFileName,newFile);
                //String uploadFile = QiNiu.uploadFile(newFile, newFileName, QiNiu.bucket, 2);
                newFile.delete();
                result.setPath(uploadFile);
                msg.setResult(result);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }catch (Exception e){
            msg = new ResponseMsg();
            msg.setResponseError(SysErrorCode.UPLOAD_ERROR);
        }
        return msg;
    }


    /**
     * 根据图片扩展名判断上传文件的类型
     * @param fileExt
     * @return
     */
    private static Integer getFileType(String fileExt) {

        // 判断是否是图片
        for(String format : SystemConstant.PIC_FORMAT) {
            if(format.equals(fileExt)) {
                return SystemConstant.PHOTO;
            }
        }

        // 判断是否是音频
        for(String format : SystemConstant.VIDEO_FORMAT) {
            if(format.equals(fileExt)) {
                return SystemConstant.VIDEO;
            }
        }

        // 判断是否是音频
        for(String format : SystemConstant.AUDIO_FORMAT) {
            if(format.equals(fileExt)) {
                return SystemConstant.AUDIO;
            }
        }

        /**
         * 判断是否是文件
         */
        for(String format : SystemConstant.TEXT_FORMAT){
            if (format.equals(fileExt)){
                return  SystemConstant.TEXT;
            }
        }

        return null;
    }






}
